/*
 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "board.h"
#include <stdio.h>
#include <stdlib.h>
#include "asr_rv32.h"
#include "asr_pinmux.h"
#include "asr_uart.h"
#include "printf_uart.h"
#if 0
#include "los_config.h"
#include "los_debug.h"
#include "los_interrupt.h"
#include "los_task.h"
#include "los_tick.h"
#else
#include "cmsis_os.h"
#include "lega_at_api.h"
#endif
#include "los_exc_info.h"
#include "alto_version.h"
#include "asr_flash.h"
#include "iot_flash.h"


/*
main task stask size(byte)
*/
#define OS_MAIN_TASK_STACK (4096)

osThreadAttr_t g_main_task={"main_task",0,NULL,0,NULL,OS_MAIN_TASK_STACK,15,0,0};
osThreadAttr_t g_flash_task={"flash_task",0,NULL,0,NULL,OS_MAIN_TASK_STACK,15,0,0};

extern void HalPendSV(void);
void PendSV_Handler(void)
{
    HalPendSV();
}
#ifdef MQTTV5_DEMO
extern void mqttv5_demo_init(void);
#endif
static void sys_init(void)
{

#ifdef RELEASE_DOMAIN
    //LOG("log_disable");
    lega_log_disable();
#endif
    alto_flash_kv_init();

    board_after_init();
    //debug_memory_access_err_check(0x0, 0x10000, MPU_AP_NA_NA);

#ifdef SYSTEM_COREDUMP
    //coredump_command_register(0, NULL);
#endif

#ifdef CFG_DUAL_AP
    comm_wifi_command_register(0,NULL);
#endif

    lega_at_init(AT_TASK_NAME,AT_TASK_PRIORITY,AT_TASK_STACK_SIZE);
    lega_at_cmd_register_all();

#ifdef ASR_PROFILE_ON
    asr_profile_config();
#endif

#ifdef LWIP_APP_IPERF
    lega_wifi_iperf_init();
#endif
#ifdef MQTTV5_DEMO
    mqttv5_demo_init();
#endif
#ifdef BLE_APP_AT_CMD
    extern void lega_ble_at_init(void);
    lega_ble_at_init();
#endif

#ifdef CFG_MRFOTA_TEST
    //extern void lega_rfota_wifi_test_at_init(void);
    //lega_rfota_wifi_test_at_init();
    //extern void lega_rfota_ble_test_at_init(void);
    //lega_rfota_ble_test_at_init();
#endif
	
    flash_test();
    printf("sys_init running...\n");
}

char test_string[] = "this is a flash test program!";

void flash_test(void){
	
	int32_t ret;
    unsigned int offset = 0x00;
	char rd_buf[32];
	printf("flash_test.....\r\n");
	ret = IoTFlashInit(); 
	
    ret = IoTFlashErase(offset, strlen(test_string));
	if(ret == 0){
        printf("flash erase success\r\n");
    }
	
	printf("flash write start offset is %d\r\n", offset);
	ret = IoTFlashWrite(offset, strlen(test_string),test_string,0);
	if(ret != 0){
        printf("flash write failed\r\n");
        return;
    }
    else{
        printf("flash write sucessful \r\n");
        printf("flash write end offset is %d\r\n", offset);
    }
	memset(&rd_buf, 0, sizeof(rd_buf));
    offset = 0x00;
    printf("flash read start offset is %d\r\n", offset);
	ret = IoTFlashRead(offset, strlen(test_string),rd_buf);
	if(ret == 0)
    {
        printf("flash read buffer data is: %s\r\n", rd_buf);
        printf("flash read end offset is %d\r\n", offset);
    }
    else
    {
        printf("flash read failed\r\n");
        return;
    }
    printf("flash test all pass!!\r\n");
   
	
}

int main(void)
{
    osStatus_t ret;
    osThreadId_t threadId;
    osThreadId_t flashtest;
    board_before_init();
    //board_uart_init();

    board_init();
    board_uart_init();
    printf("\r\n version app:%s\r\n", LEGA_VERSION_STR);
#if LOSCFG_PLATFORM_EXC == 1
    OsExcMsgDumpInit();
#endif

#ifdef DEBUG_BACKTRACE
    debug_backtrace_init();
#endif // DEBUG_BACKTRACE
    ret = osKernelInitialize();

    if(ret == osOK)
    {
        threadId = osThreadNew((osThreadFunc_t)sys_init,NULL,&g_main_task);
		if(threadId!=NULL)
        {
            osKernelStart();
			
        }
		//flashtest = osThreadNew((osThreadFunc_t)flash_test,NULL,&g_flash_task);
    }  
	

    while(1);    
    return 0;
}
